import cv2
import numpy as np

# 读取图片
image = cv2.imread('caise.jpg')

# 分离 B、G、R 通道
b, g, r = cv2.split(image)

# 获取图像尺寸（高度、宽度）
h, w = b.shape

# 方法1：直接创建三通道数组（推荐，更直观）
# 蓝通道图像：B通道保留，G和R通道为0
b_channel = np.zeros((h, w, 3), dtype=np.uint8)  # 初始化三通道全0数组
b_channel[:, :, 0] = b

# 绿通道图像：G通道保留，B和R通道为0
g_channel = np.zeros((h, w, 3), dtype=np.uint8)
g_channel[:, :, 1] = g

# 红通道图像：R通道保留，B和G通道为0
r_channel = np.zeros((h, w, 3), dtype=np.uint8)
r_channel[:, :, 2] = r


cv2.imshow('Blue Channel', b_channel)
cv2.imshow('Green Channel', g_channel)
cv2.imshow('Red Channel', r_channel)
cv2.waitKey(0)


cv2.imwrite('Blue_channel.jpg', b_channel)
cv2.imwrite('Green_channel.jpg', g_channel)
cv2.imwrite('Red_channel.jpg', r_channel)